//
//  ZTXProjectListCodeController.m
//  NtoccDriverNew
//
//  Created by 张天兴 on 2023/10/19.
//  Copyright © 2023 GCY. All rights reserved.
//

#import "ZTXProjectListCodeController.h"
#import "SupplyModel.h"
#import "ZTXYunDanProjectListCell.h"
#import "ZTXJieDanBottomView.h"
#import "HuoYuanDetailViewController.h"

@interface ZTXProjectListCodeController ()<UITableViewDataSource, UITableViewDelegate,ZTXProjectDelegate>

@property (strong , nonatomic)UITableView *tableView;

@property (strong , nonatomic)NSMutableArray *dataArray;

@property (strong , nonatomic)UIImageView *foundImageView;

@property (nonatomic, assign) NSInteger pageNum;

@property (nonatomic, strong) UIView *bgView;

@property (nonatomic, strong) UITapGestureRecognizer *changeVoidceSet;

@property (nonatomic, strong) ZTXJieDanBottomView *jieDanView;

@property (nonatomic, strong) NSString *currentCode;

@end
static NSString *const cellID = @"ZTXYunDanProjectListCell";

static const CGFloat cellHeight = 250;

@implementation ZTXProjectListCodeController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    _pageNum = 1;
    
    [self setupBaseView];
    
    [self freightIndexListRequest];
}

- (void)setupBaseView {
    
    self.navigationItem.title = @"路线订单列表";
    
    self.view.backgroundColor = BackgroundColor;
    
    [self.view addSubview:self.tableView];
}

- (void)freightIndexListRequest {
    
    WEAKSELF
    if (_pageNum == 1) {
        [self.dataArray removeAllObjects];
    }
    
    NSDictionary *params = @{
        @"pageNum": [NSNumber numberWithInteger:_pageNum],
        @"pageSize": DEFULT_LOAD_NUM,
        @"projectCode":self.projectCode
    };
    
    
    [HTTPManager getNetworkRequest:CJStringWithFormat(@"%@%@", [NSString stringWithFormat:@"%@/ntocc-tms-api",[ReAddressDynamic shared].baseUrl], ORDER_PROJECTLIST) withParameter:params withVC:self netName:@"项目接单列表" success:^(id response, NSString *msg) {
        
        if ([response[@"records"] count] != 0) {

            [weakSelf.dataArray addObjectsFromArray:[YunDanCenterModel mj_objectArrayWithKeyValuesArray:response[@"records"]]];
            [weakSelf.tableView.mj_header endRefreshing];
            [weakSelf.tableView.mj_footer endRefreshing];

        }else{
            if (_pageNum == 1) {
                weakSelf.foundImageView.hidden = NO;
                weakSelf.tableView.hidden = YES;
            }

            [weakSelf.tableView.mj_header endRefreshing];
            [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];

        }

        [weakSelf.tableView reloadData];
        
    } failure:^(NSInteger code, NSString *msg) {
        
    }];
}

//上拉刷新数据
- (void)loadNewData{
    _pageNum = 1;
    [self freightIndexListRequest];
}

- (void)loadMoreData{
    _pageNum ++;
    [self freightIndexListRequest];
}


#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ZTXYunDanProjectListCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell.jieDanButton addTarget:self action:@selector(jieDanButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    cell.supplyModel = self.dataArray[indexPath.row];
    
    return cell;
}

#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return cellHeight;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.001f;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    YunDanCenterModel *model = self.dataArray[indexPath.row];
    self.currentCode = model.code;
    // 直接跳转货源详情
    HuoYuanDetailViewController *hydVC = [HuoYuanDetailViewController new];
    hydVC.orderCode = model.code;
    hydVC.type = @"1";
    [self.navigationController pushViewController:hydVC animated:YES];
}


#pragma mark - LazyLoad
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.frame = CGRectMake(0, 10, ScreenW, ScreenH - NavHeight - MenuBarHeight - 10);
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor whiteColor];
        
        //注册单元格
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([ZTXYunDanProjectListCell class]) bundle:nil] forCellReuseIdentifier:cellID];
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
        _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    }
    
    return _tableView;
}

- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}
- (UIImageView *)foundImageView {
    if (!_foundImageView) {
        _foundImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 170, 170)];
        _foundImageView.image = image(@"gcy_img_list_found");
        _foundImageView.contentMode = UIViewContentModeScaleAspectFit;
        _foundImageView.cy_centerX = self.view.cy_centerX;
        _foundImageView.cy_centerY = self.view.cy_centerY;
        [self.view addSubview:_foundImageView];
    }
    return _foundImageView;
}
- (UIView *)bgView{
    if(!_bgView){
        self.bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH)];
        self.bgView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.6];
        self.changeVoidceSet = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(changeVoidceSet:)];
        [self.bgView addGestureRecognizer:self.changeVoidceSet];
        [self.bgView addSubview: self.jieDanView];
    }
    return _bgView;
}
- (ZTXJieDanBottomView *)jieDanView{
    if(!_jieDanView){
        self.jieDanView = [[ZTXJieDanBottomView alloc] initWithFrame:CGRectMake(20, (ScreenH - 150)/2, ScreenW - 40, 150)];
        self.jieDanView.layer.masksToBounds = YES;
        self.jieDanView.layer.cornerRadius = 10;
        self.jieDanView.backgroundColor = [UIColor whiteColor];
        self.jieDanView.delegate = self;
    }
    return _jieDanView;
}
- (void)jieDanButtonAction:(UIButton *)btn{
    CGPoint point = btn.center;//找到btn中心点记录下来
    point = [self.tableView convertPoint:point fromView:btn.superview];
    NSIndexPath* indexpath = [self.tableView indexPathForRowAtPoint:point];
    YunDanCenterModel *model = self.dataArray[indexpath.row];
    self.currentCode = model.code;
    // 直接跳转货源详情
    HuoYuanDetailViewController *hydVC = [HuoYuanDetailViewController new];
    hydVC.orderCode = model.code;
    hydVC.type = @"1";
    [self.navigationController pushViewController:hydVC animated:YES];
//    if([model.coalUnit isEqualToString:@"0"]){
//        [[UIApplication sharedApplication].keyWindow addSubview:self.bgView];
//    }else{
//        NSString *url = CJStringWithFormat(@"%@%@",[NSString stringWithFormat:@"%@/ntocc-tms-api",[ReAddressDynamic shared].baseUrl], ORDERDETAIL_WECHATRECEIVE);
//        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
//        [dic setValue:self.currentCode forKey:@"mainOrderCode"];
//        [dic setValue:[UserDefaultUtil valueForKey:DriverPartyCode] forKey:@"driverCodes"];
////        [dic setValue:numberStr forKey:@"perWeight"];
//        [HTTPManager postNetworkRequest:url withParameter:dic withVC:self netName:@"项目货源接单" success:^(id response, NSString *msg) {
//            [XSInfoView showInfo:@"接单成功" onView:self.view];
//            [self.bgView removeFromSuperview];
//            [self.navigationController popToRootViewControllerAnimated:YES];
//        } failure:^(NSInteger code, NSString *msg) {
//
//        }];
//    }
}
- (void)ZTXProjectDelegateNumbdr:(NSString *)numberStr{
    NSString *url = CJStringWithFormat(@"%@%@",[NSString stringWithFormat:@"%@/ntocc-tms-api",[ReAddressDynamic shared].baseUrl], ORDERDETAIL_WECHATRECEIVE);
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setValue:self.currentCode forKey:@"mainOrderCode"];
    [dic setValue:[UserDefaultUtil valueForKey:DriverPartyCode] forKey:@"driverCodes"];
    [dic setValue:numberStr forKey:@"perWeight"];
    [HTTPManager postNetworkRequest:url withParameter:dic withVC:self netName:@"项目货源接单" success:^(id response, NSString *msg) {
        [XSInfoView showInfo:@"接单成功" onView:self.view];
        [self.bgView removeFromSuperview];
        [self.navigationController popToRootViewControllerAnimated:YES];
    } failure:^(NSInteger code, NSString *msg) {
        
    }];
}
- (void)changeVoidceSet:(UITapGestureRecognizer *)sender {
    CGPoint location = [sender locationInView:self.jieDanView];
    DebugLog(@"--x:%f    y:%f",location.x,location.y);
    if (location.y <= (ScreenH - 150)/2 || location.y >= (ScreenH - 150)/2 + 120) {
        [self.bgView removeFromSuperview];
    }else{
       
    }

}
@end
